#-*- coding: utf-8 -*-

"""
description	: 2013 HS-RM ComputerVision Schwanecke, Uebung5

date			:201311
version		: 
autor           : Pascal Trebert + U. Schwanecke
usage		: python regions.py

Python 2.7.4

"""

import cv2, sys, numpy as np

from PyQt4 import QtCore
from PyQt4 import Qt
from PyQt4 import QtGui


class Pic(QtGui.QWidget):
    """Ein Bild auf dem verschiedene Regions-Bearbeitungen moeglich sind"""

    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)

        self.name = None
        self.img = None
        self.bounding_box = []
        self.num_of_black_pixel = 0
        self.area_of_all_black_pixel = 0
        self.size_of_one_pixel = 0.085**2

        # zum lowlevel Zeichnen
        self.painter = QtGui.QPainter()

        # Layout
        self.create_widgets()


    def create_widgets(self):
        main_layout = QtGui.QHBoxLayout()  # Horizontal 2 geteiltes Bild
        self.setLayout(main_layout)

        #############
        # Links
        #############
        # 1. Zeile (Unterlayout)
        left_line_01 = QtGui.QHBoxLayout()
        main_layout.addLayout(left_line_01)

        # Button
        self.b_get_black_pixel_area = QtGui.QPushButton(u"Pixelfläche", self)
        Qt.QObject.connect(self.b_get_black_pixel_area, Qt.SIGNAL('clicked()'), self.get_black_pixel_area)
        left_line_01.addWidget(self.b_get_black_pixel_area)

        # Label
        self.label_black_pixel_area = QtGui.QLabel(u"Label", self)
        left_line_01.addWidget(self.label_black_pixel_area)

        #############
        # Rechts
        #############
        # Nur das Bild anzeigen
        right_line_00 = QtGui.QHBoxLayout()
        main_layout.addLayout(right_line_00)

        # Bild einlesen und anzeigen
        self.img = QtGui.QLabel()
        self.img.setPixmap(QtGui.QPixmap("partsbw.png"))
        print 'size: %s\twidth%s\theight:%s' % (self.img.size(), self.img.width(), self.img.height())
        right_line_00.addWidget(self.img)

    # QMouseEvent
    def mousePressEvent(self, event):

        # 0x1 - rechte Maustaste
        # 0x2 - linke Maustaste
        # 0x4 - mittlere Maustaste
        button = event.button()
        pos = event.globalPos()
        global_x = event.globalX()
        global_y = event.globalY()

        print 'real', button.real
        print 'numerator', button.numerator
        rectangle = QtCore.QRectF(10.0, 20.0, 80.0, 60.0)
        painter = QtGui.QPainter()
        painter.drawEllipse(rectangle)

        #print 'button ', button
        #print 'type(button', type(button)
        #print 'mouse pressed'
        #print 'x()', event.x()
        #print 'y()', event.y()

        #print 'type event.button()', type(event.button())
        #print 'event.button()', event.button()
        if button.real is 0x1:
            print 'LM'
            if len(self.bounding_box) < 5:
                self.bounding_box.append((global_x, global_y))
                self.painter.drawEllipse(pos, 5, 5)
        elif button.real is 0x2:
            print 'RM'
        elif button.real is 0x4:
            print 'MM'
        #length = len(points)+1


        #if length<5:
        #    # Klickpos rot markieren
        #    cv2.circle(image,(x,y),10,COLOR,-1)
        #    points.append((x,y))
        #    cv2.imshow(PIC_NAME,image)
        #
        #    # Linien einzeichnen
        #    if length==2:
        #        draw_line(PIC_NAME, image, *points[:2])
        #    elif length==3:
        #        draw_line(PIC_NAME, image, *points[1:3])
        #    elif length==4:
        #        draw_line(PIC_NAME, image, *points[2:4])
        #        draw_line(PIC_NAME, image, points[0], points[3])
        #        # Ausschnitt anzeigen
        #        perspective_transform(image, points)


    # Button Callback-Methoden
    def get_black_pixel_area(self, bounding_box=None, pic=None):
        """Berrechnet die Groesse aller schwarzen Pixel im Bild"""
        print 'click'
        bounding_box = self.bounding_box if bounding_box is None else bounding_box
        pic = self.img if pic is None else pic



class MyMainWindow(QtGui.QMainWindow):
    """Hauptfenster in dem ein Bild zur Bearbeitung dargestellt wird"""

    def __init__(self, *args):
        QtGui.QMainWindow.__init__(self, *args)
        self.setWindowTitle('Region Processing')

        self.pic = Pic(self)
        self.setCentralWidget(self.pic)
        #self.adjustSize()





if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv) # genau einmal pro Anwendung
    w = MyMainWindow()
    w.show()
        
    sys.exit(app.exec_())
